var util = require('../../utils/util');
var util2 = require('../../utils/util2');
Page({
  data: {
    user: {},
    patientInfo: {},
    doctorInfo: {},
    today: util.formatTime(new Date()),
    beizhu: ''
  },
  onLoad(options) {
    var that = this
    //从预约按钮获取医生的doctorId
    console.log('这个医生的Id', options.doctorId)
    //根据doctorId拿到医师的信息
    wx.cloud.callFunction({
      name: 'doctorInfo',
      data: {
        action: 'findOneId',
        doctorId: options.doctorId,
      }
    }).then(res => {
      console.log('拿到这个医师的信息成功', res)
      that.setData({
        doctorInfo: res.result.data[0]
      })
    }).catch(res => {
      console.log('拿到这个医师的信息失败', res)
    })
    //一进来就拿用户信息
    let user = wx.getStorageSync('user')
    that.setData({
      user: user
    })
    console.log(user.admin)
    //根据user的admin去patientInfo找用户姓名
    wx.cloud.callFunction({
      name: 'patientInfo',
      data: {
        action: 'findOne',
        admin: user.admin
      }
    }).then(res => {
      console.log('根据admin找用户姓名成功', res)
      that.setData({
        patientInfo: res.result.data[0]
      })
    }).catch(res => {
      console.log('根据admin找用户姓名失败', res)
    })

  },
  //添加备注
  getBeizhu(e) {
    // console.log(e.detail.value)
    this.setData({
      beizhu: e.detail.value.replace(/\s+/g, '')
    })
  },
  //提交预约
  commit() {
    var that = this
    //获取此人排号
    let today2 = util2.formatTime(new Date())
    let lastNumberString = String(that.data.doctorInfo.lastNumber)
    let lastNumberKey = lastNumberString.indexOf(today2)
    console.log('lastNumberKey', lastNumberKey)
    let number = lastNumberKey < 0 ? parseInt(today2 + "001", 10) : parseInt(that.data.doctorInfo.lastNumber + 1, 10)
    //新建排号信息
    wx.cloud.callFunction({
      name: 'appoint',
      data: {
        action: 'add',
        patientInfo: that.data.patientInfo,
        doctorInfo: that.data.doctorInfo,
        user: that.data.user,
        patientAdmin: that.data.patientInfo.admin,
        doctorId: that.data.doctorInfo.doctorId,
        appointDay: that.data.today,
        beizhu: that.data.beizhu.length==0?'无备注':that.data.beizhu,
        isOver: false,
        isPassed: false,
        flowId: that.data.doctorInfo.doctorId + that.data.user.admin + number,
        number: number
      }
    }).then(res => {
      console.log('新建预约信息成功', res)
      //医生的最后排号也要更新
      wx.cloud.callFunction({
        name: "doctorInfo",
        data: {
          action: 'changeLastNumber',
          doctorId: that.data.doctorInfo.doctorId,
          lastNumber: number
        }
      }).then(res => {
        console.log('改变医生最后排位成功', res)
        //成功提示
        wx.showToast({
          title: '预约成功',
        })
        //跳转到查看
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/seeMyAppoint/seeMyAppoint?flowId=' + that.data.doctorInfo.doctorId + that.data.user.admin + number,
          })
        }, 1000)
      }).catch(res => {
        console.log('改变医生最后排失败', res)
        //失败提示
        wx.showToast({
          icon: 'none',
          title: '预约失败，请检查网络',
        })
      })
    }).catch(res => {
      console.log('新建预约信息失败', res)
      //失败提示
      wx.showToast({
        icon: 'none',
        title: '预约失败，请检查网络',
      })
    })
  }
})